//
//  XMTabBar.m
//  XMWeibo
//
//  Created by chi on 14-8-19.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMTabBar.h"
#import "XMTabBarButton.h"

@interface XMTabBar ()

//当前选中图片
@property (nonatomic, weak) XMTabBarButton *selectedButton;

//tabbarbutton数组
@property (nonatomic, strong) NSMutableArray *tabbarButtons;

//加号按钮
@property (nonatomic, weak) UIButton *addButton;

@end


@implementation XMTabBar

- (NSMutableArray *)tabbarButtons
{
    if (_tabbarButtons == nil) {
        _tabbarButtons = [NSMutableArray array];
    }
    
    return _tabbarButtons;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        if (!isIOS7) {
            self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background"]];
        }
        
        [self setupAddButton];
        
    }
    return self;
}

//添加中间的加号按钮
- (void)setupAddButton
{
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置背景
    [addButton setBackgroundImage:[UIImage adaptIOS7ImageWithName:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [addButton setBackgroundImage:[UIImage adaptIOS7ImageWithName:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];

    [addButton setImage:[UIImage adaptIOS7ImageWithName:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [addButton setImage:[UIImage adaptIOS7ImageWithName:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    
    addButton.bounds = (CGRect){CGPointZero, addButton.currentBackgroundImage.size};
    self.addButton = addButton;
    [addButton addTarget:self action:@selector(clickAddButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:addButton];
}

/**
 *  点击新建微博按钮
 */
- (void)clickAddButton:(UIButton*)button
{
    if ([self.delegate respondsToSelector:@selector(clickNewWeibboButton)]) {
        [self.delegate clickNewWeibboButton];
    }
}
         
/**
 *  添加tabarbutton
 */
- (void)addTabBarButtonWithItem:(UITabBarItem *)item
{
    XMTabBarButton *tabbarBtn = [[XMTabBarButton alloc]init];
    tabbarBtn.tabBarItem = item;
    [tabbarBtn addTarget:self action:@selector(clickTabBarButton:) forControlEvents:UIControlEventTouchDown];
    
    [self addSubview:tabbarBtn];
    
    
    [self.tabbarButtons addObject:tabbarBtn];
    
    
    if (self.tabbarButtons.count == 1) {
        [self clickTabBarButton:tabbarBtn];
    }
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    
    self.addButton.center = CGPointMake(width * 0.5, height * 0.5);
    
    
    NSUInteger subviewCount = self.tabbarButtons.count;
    if (subviewCount > 0) {
        CGFloat viewW = self.bounds.size.width / self.subviews.count;
        CGFloat viewH = height;
        CGFloat viewX = 0.0;
        CGFloat viewY = 0.0;
        
        for (int i = 0; i < subviewCount; ++i) {
            if (i > 1) {
                viewX = (i + 1) * viewW;
            }
            else {
                viewX = i * viewW;
            }
            
            
            
            XMTabBarButton *btn = self.tabbarButtons[i];

            btn.frame = CGRectMake(viewX, viewY, viewW, viewH);
            btn.tag = i;
        }
    }
    

    
//    if (subviewCount > 0) {
//        CGFloat viewW = self.bounds.size.width / subviewCount;
//        CGFloat viewH = self.bounds.size.height;
//        CGFloat viewY = 0.0;
//        for (int i = 0; i < subviewCount; ++i) {
//            XMTabBarButton *btn = self.subviews[i];
//            btn.frame = CGRectMake(viewW * i, viewY, viewW, viewH);
//            btn.tag = i;
//        }
//        
//    }
}

- (void)clickTabBarButton:(XMTabBarButton*)button
{
    [self.selectedButton setSelected:NO];
    
    button.selected = YES;
    
    if ([self.delegate respondsToSelector:@selector(selectTabBarFrom:To:)]) {
        [self.delegate selectTabBarFrom:self.selectedButton.tag To:button.tag];
    }
    
    
    self.selectedButton = button;
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"KVO come here");
}

@end
